package com.service;

import com.domain.Buns;
import com.domain.Goodpic;
import com.domain.result.ResultData;
import com.mapper.primary.BunsMapper;
import com.mapper.primary.GoodpicMapper;
import com.service.seinterface.BunsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.List;

@Service
public class BunsServiceImpl implements BunsService {

    @Resource
    BunsMapper bunsMapper;

    @Resource
    GoodpicMapper goodpicMapper;
    @Value(value = "${file.location}")
    public String filepath;

    @Transactional
    @Override
    public ResultData addBuns(Buns buns, MultipartFile file) {
        ResultData resultData=new ResultData();
        int count=bunsMapper.insert(buns);
        if(count<=0){
            resultData.setResult(1);
            resultData.setMessage("添加失败");
            return resultData;
        }
        //保存图片
        if(file!=null&&!file.isEmpty()){
            System.out.println(file.getOriginalFilename());
            File currentFile=new File(filepath,file.getOriginalFilename());
            try {
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(currentFile));
                out.write(file.getBytes());
                out.flush();
                out.close();
                Goodpic goodpic=new Goodpic();
                goodpic.setFilepath(file.getOriginalFilename());
                goodpic.setGoodid(buns.getBunsid());
                goodpicMapper.insert(goodpic);
                resultData.setMessage("添加成功");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                resultData.setResult(1);
                resultData.setMessage("上传失败");
                return resultData;
            } catch (IOException e) {
                e.printStackTrace();
                resultData.setResult(1);
                resultData.setMessage("上传失败");
                return resultData;
            }
        }else{
            Goodpic goodpic=new Goodpic();
            goodpic.setFilepath("");
            goodpic.setGoodid(buns.getBunsid());
            goodpicMapper.insert(goodpic);
        }

        System.out.println("id"+buns.getBunsid());
        return resultData;
    }

    @Override
    public ResultData deleteBunsById(Integer bunsid) {
        ResultData resultData=new ResultData();
        int count=bunsMapper.deleteByPrimaryKey(bunsid);
        if(count<=0){
            resultData.setResult(1);
            resultData.setMessage("删除失败");
        }else{
            resultData.setMessage("删除成功");
        }
        return resultData;

    }

    @Override
    public ResultData updateBunsById(Buns buns,MultipartFile file) {

        ResultData resultData=new ResultData();
        int count=bunsMapper.updateByPrimaryKey(buns);
        if(count<=0){
            resultData.setResult(1);
            resultData.setMessage("修改失败");
            return resultData;
        }
        //保存图片
        if(file!=null&&!file.isEmpty()){
            System.out.println(file.getOriginalFilename());
            File currentFile=new File(filepath,file.getOriginalFilename());
            try {
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(currentFile));
                out.write(file.getBytes());
                out.flush();
                out.close();
                Goodpic goodpic=new Goodpic();
                goodpic.setFilepath(file.getOriginalFilename());
                goodpic.setGoodid(buns.getBunsid());
                goodpicMapper.updateByPrimaryKey(goodpic);
                resultData.setMessage("修改成功");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                resultData.setResult(1);
                resultData.setMessage("上传失败");
                return resultData;
            } catch (IOException e) {
                e.printStackTrace();
                resultData.setResult(1);
                resultData.setMessage("上传失败");
                return resultData;
            }
        }
        return resultData;
    }

    @Override
    public ResultData selectBunsAll() {
        ResultData resultData=new ResultData();
        resultData.setData(bunsMapper.selectAll());
        return resultData;

    }

    @Transactional
    @Override
    public ResultData deleteBunsByIds(List<Integer> list) {
        ResultData resultData=new ResultData();
        if(list==null||list.size()==0){
            resultData.setResult(1);
            resultData.setMessage("请传入商品id");
            return resultData;
        }
        try {
            for(Integer id:list){
                int count=bunsMapper.deleteByPrimaryKey(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultData.setResult(1);
            return resultData;
        }
        return resultData;
    }
}
